//Example - 1
function add1(){
if (arguments.length == 0){
document.write("NO Argument Passed!");
}
else{
let sum = 0;
for(let num in arguments){
sum += arguments[num];
}
document.write(sum);
}
}
add1(5,10);
In this example, I am getting the correct result.
//Example - 2 ( With Arrow Function)
let sum = () => {
if( arguments.length == 0){
document.write("NO Argument Passed!");
}
else{
let sum1 = 0;
for(let number in arguments){
sum1 += arguments[number];
}
document.write(sum1);
}
};
sum(10,20);
*Here I am getting an error in console "arguments is not defined". Please tell me where I did Wrong.Thank you..!
This is because arrow functions do not have argument bindings.
option 1 changing arrow function to take an array arguments parameter
let sum = (arguments) => {
if( arguments.length == 0){
console.log("NO Argument Passed!");
}
else{
let sum1 = 0;
for(let number in arguments){
sum1 += arguments[number];
}
console.log(sum1);
}
};
sum([10,20]);
option 2 rest parameters which allows indefinite amount of arguments
let sum = (...arguments) => {
if( arguments.length == 0){
console.log("NO Argument Passed!");
}
else{
let sum1 = 0;
for(let number in arguments){
sum1 += arguments[number];
}
console.log(sum1);
}
};
sum(10,20);